package com.zyp.demo.web.admin;

import com.zyp.demo.MD5util.MD5Utils;
import com.zyp.demo.po.User;
import com.zyp.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private UserService userService;
    @RequestMapping
    public String LoginController(){
        return  "admin/login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        HttpSession session,
                        RedirectAttributes attributes
                        ){
        if ("root".equals(username) && "123456".equals(password)){
            session.setAttribute("user","root");
            return "admin/index";
        }
        User user = userService.checkUser(username, password);
        System.out.println(username+password);
        if(user!=null){
            user.setPassword(null);
            session.setAttribute("user",user);
            //session.setMaxInactiveInterval();
            return "admin/index";

        }
        attributes.addFlashAttribute("message","用户名或密码错误");
        return "redirect:/admin";
    }
    @GetMapping("/outlog")
    public String outlog(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/admin";
    }
    @GetMapping("/index")
    public  String index(){
        return "/admin/index";
    }
}
